﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using Gman.WebApp.GestionServiceRef;
using Gman.Entity.Entities;
using Gman.WebApp.ServiceControllers;
using AutoMapper;

namespace Gman.WebApp.ServiceControllers
{
    public class GestionServiceController : ServiceControllerBase
    {
        public void TestAutomapper(){
            var bien = new BienBE();
            bien.DescBien = "desc";
            bien.AreaBien = "area";
            bien.CaractBien = "caract";            

            BienBE bienCopy = Mapper.Map<BienBE, BienBE>(bien);
        }



        #region Diagnostico

        public IList<DiagnosticoBE> ListarDiagnostico(DiagnosticoBE diagnostico)
        {
            try
            {
                Request_Diagnostico request = new Request_Diagnostico();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListDiagnostico" };
                request.DiagnosticoDTO = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(diagnostico);

                Response_Diagnostico response = GestionServiceClient.ListarDiagnostico(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstDiagnostico;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int RegistrarDiagnostico(DiagnosticoBE diagnostico, List<DiagnosticoDetalleBE> lista)
        {
            try
            {
                Request_Diagnostico request = new Request_Diagnostico();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "AddDiagnostico" };
                request.DiagnosticoDTO = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(diagnostico);

                request.listDiagnosticoDetalleBE = lista.ToArray();

                Response_Diagnostico response = GestionServiceClient.RegistrarDiagnostico(request);

                if (request.DiagnosticoDTO.IDDiagnostico > 0)
                {
                    diagnostico.IDDiagnostico = request.DiagnosticoDTO.IDDiagnostico;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsDiagnostico;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public DiagnosticoBE ObtenerDiagnostico(DiagnosticoBE diagnostico)
        {
            try
            {
                Request_Diagnostico request = new Request_Diagnostico();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetDiagnostico" };
                request.DiagnosticoDTO = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(diagnostico);

                Response_Diagnostico response = GestionServiceClient.ObtenerDiagnostico(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetDiagnostico;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int ActualizarDiagnostico(DiagnosticoBE diagnostico, List<DiagnosticoDetalleBE> list, List<DiagnosticoDetalleBE> toDelete)
        {
            try
            {
                Request_Diagnostico request = new Request_Diagnostico();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "UpdDiagnostico" };
                request.DiagnosticoDTO = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(diagnostico);

                request.listDiagnosticoDetalleBE = list.ToArray();
                request.toDelete = toDelete.ToArray();


                Response_Diagnostico response = GestionServiceClient.ActualizarDiagnostico(request);

                if (request.DiagnosticoDTO.IDDiagnostico > 0)
                {
                    diagnostico.IDDiagnostico = request.DiagnosticoDTO.IDDiagnostico;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdDiagnostico;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion



        #region DiagnosticoDetalle

        public IList<DiagnosticoDetalleBE> ListarDiagnosticoDetalle(DiagnosticoDetalleBE diagnosticoDetalle)
        {
            try
            {
                Request_DiagnosticoDetalle request = new Request_DiagnosticoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListDiagnosticoDetalle" };
                request.DiagnosticoDetalleDTO = Mapper.Map<DiagnosticoDetalleBE, DiagnosticoDetalleBE>(diagnosticoDetalle);

                Response_DiagnosticoDetalle response = GestionServiceClient.ListarDiagnosticoDetalle(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstDiagnosticoDetalle;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        #endregion




        #region ot

        public OrdenTrabajoBE ObtenerOTXNro(OrdenTrabajoBE oItem)
        {
            try
            {
                Request_OrdenTrabajo request = new Request_OrdenTrabajo();
                request.OrdenTrabajoDTO = oItem;

                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "ObtenerOTxNro" };

                Response_OrdenTrabajo response = GestionServiceClient.ObtenerOTxNro(request);


                return response.GetOrdenTrabajo;
            }
            catch (Exception ex)
            {
                throw new Exception("ConceptoBL.ObtenerConceptosXtipo()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }


        }

        #endregion




        #region Asnc

        public int RegistrarAsnc(ActaServNoConformeBE asnc)
        {
            try
            {
                Request_ActaServNoConforme request = new Request_ActaServNoConforme();
                request.ActaServNoConformeDTO = asnc;

                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;

                request.LoadOptions = new string[] { "Add" };
                Response_ActaServNoConforme response = GestionServiceClient.RegistrarAsnc(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsActaServNoConforme;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int ActualizarAsnc(ActaServNoConformeBE asnc)
        {
            try
            {
                Request_ActaServNoConforme request = new Request_ActaServNoConforme();
                request.ActaServNoConformeDTO = asnc;

                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;

                request.LoadOptions = new string[] { "update" };
                Response_ActaServNoConforme response = GestionServiceClient.ActualizarAsnc(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdActaServNoConforme;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }



        public ActaServNoConformeBE ObtenerAsncXNro(ActaServNoConformeBE oItem)
        {
            try
            {
                Request_ActaServNoConforme request = new Request_ActaServNoConforme();
                request.RequestId = NewRequestId;
                request.ActaServNoConformeDTO = oItem;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "obtenerAsnc" };
                //request.ProveedorDTO = Mapper.Map<ProveedorBE, ProveedorBE>(oItem);
                Response_ActaServNoConforme response = GestionServiceClient.ObtenerXNroAsnc(request);

                return response.GetActaServNoConforme;
            }
            catch (Exception ex)
            {
                throw new Exception("ProveedorBL.BuscarProveedor()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }

        }



        #endregion

    
    
    }
}